﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.DALFactory;

    public class MovementDetailBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IInventoryMgtDA inventoryMgtDA = DataAccess.CreateInventoryMgtDA();
        private static readonly ILocationDA locDA = DataAccess.CreateLocationDA();
        private static readonly ILotAttributeDA lotAttributeDA = DataAccess.CreateLotAttributeDA();
        private static readonly IMovementDetailDA movementDetailDA = DataAccess.CreateMovementDetailDA();
        private static readonly IPackDA packDA = DataAccess.CreatePackDA();
        private PubParasInfo pubParasInfo;
        private static readonly IRotationRuleDA rotataionRuleDA = DataAccess.CreateRotationRuleDA();
        private static readonly IRotationRuleDetailDA rotataionRuleDetailDA = DataAccess.CreateRotationRuleDetailDA();
        private static readonly ISkuDA skuDA = DataAccess.CreateSkuDA();

        public MovementDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public MovementDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public MovementDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteMovement(string MovementID, List<string> IDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                if (movementDetailDA.DeleteMovementDetail(dataBase, tran, MovementID, IDS, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        temp = temp + IDS[i] + ",";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public MovementDetailInfo GetAllMovementDetailByID(string MovementID, string ListID)
        {
            return movementDetailDA.GetMovementDetailByID(MovementID, ListID, this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetAllMovementDetailInfo()
        {
            return movementDetailDA.GetAllMovementDetail(this.dataBaseID[0]);
        }

        public string InsertMovementDetail(MovementDetailInfo obj)
        {
            Exception ex;
            string strMovementDetailID = string.Empty;
            try
            {
                if (obj == null)
                {
                    return strMovementDetailID;
                }
                DataBase dataBase = new DataBase();
                DbConnection conn = dataBase.connection;
                conn.Open();
                DbTransaction tran = conn.BeginTransaction();
                try
                {
                    try
                    {
                        if (!skuDA.CheckSkuIDUnique(dataBase, tran, obj.OwnerID, obj.SkuID, this.dataBaseID[0]))
                        {
                            throw new Exception("货品" + obj.SkuID + "不存在!");
                        }
                        if (!locDA.CheckLocationIDUnique(dataBase, tran, obj.FromLoc, this.dataBaseID[0]))
                        {
                            throw new Exception("来源货位" + obj.FromLoc + "不存在!");
                        }
                        if (!locDA.CheckLocationIDUnique(dataBase, tran, obj.ToLoc, this.dataBaseID[0]))
                        {
                            PublicMethod.GetInstance().ShowAlertMessage("F2330_003", this.pubParasInfo.UserLanguage, new List<string> { obj.ToLoc });
                        }
                        if (movementDetailDA.CheckMovementDetailIDUnique(dataBase, tran, obj.MovementID, obj.LineID.ToString(), this.dataBaseID[0]))
                        {
                        }
                        obj.UpdatedBy = obj.CreatedBy;
                        obj.CreatedDate = DateTime.Now;
                        obj.UpdatedDate = DateTime.Now;
                        if (movementDetailDA.InsertMovementDetail(dataBase, tran, obj, this.dataBaseID[0]) > 0)
                        {
                            strMovementDetailID = obj.LineID.ToString();
                        }
                        tran.Commit();
                    }
                    catch (Exception exception1)
                    {
                        ex = exception1;
                        tran.Rollback();
                        CommonErrorHandler.ErrorHandler(ex, LogType.Error);
                    }
                    return strMovementDetailID;
                }
                finally
                {
                    conn.Close();
                }
            }
            catch (Exception exception2)
            {
                ex = exception2;
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return strMovementDetailID;
        }

        public string MovementDetailImportByData(string[][] ImportByData)
        {
            Exception ex;
            try
            {
                int i;
                string whLoginID = this.dataBaseID[0];
                string strMovementID = string.Empty;
                Dictionary<string, SkuInfo> skuObjects = new Dictionary<string, SkuInfo>();
                Dictionary<string, RotationRuleInfo> rotationRuleObjects = new Dictionary<string, RotationRuleInfo>();
                Dictionary<string, PackInfo> packInfoObjects = new Dictionary<string, PackInfo>();
                OutboundMgtBLL outboundMgtBLL = new OutboundMgtBLL(this.pubParasInfo);
                List<MovementDetailInfo> movementDetailInfoList = new List<MovementDetailInfo>();
                Dictionary<string, InventoryInfo> allocatedInventoryInfoList = new Dictionary<string, InventoryInfo>();
                for (i = 1; i < ImportByData.Length; i++)
                {
                    MovementDetailInfo movementDetailInfo = new MovementDetailInfo {
                        CreatedBy = this.pubParasInfo.UserID,
                        UpdatedBy = this.pubParasInfo.UserID
                    };
                    for (int j = 0; j < ImportByData[i].Length; j++)
                    {
                        switch (ImportByData[0][j].ToUpper().Trim())
                        {
                            case "OWNERID":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    movementDetailInfo.OwnerID = ImportByData[i][j];
                                }
                                continue;
                            }
                            case "ORDERID":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    movementDetailInfo.MovementID = ImportByData[i][j];
                                    strMovementID = movementDetailInfo.MovementID;
                                }
                                continue;
                            }
                            case "SKU":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    movementDetailInfo.SkuID = ImportByData[i][j];
                                }
                                continue;
                            }
                            case "LOC":
                            {
                                if (string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    break;
                                }
                                movementDetailInfo.FromLoc = ImportByData[i][j];
                                continue;
                            }
                            case "LOT":
                            {
                                if (string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    goto Label_0339;
                                }
                                movementDetailInfo.Lot = ImportByData[i][j];
                                continue;
                            }
                            case "LPN":
                            {
                                if (string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    goto Label_0372;
                                }
                                movementDetailInfo.FromLpnID = ImportByData[i][j];
                                continue;
                            }
                            case "QTY":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    movementDetailInfo.FromQty = decimal.Parse(ImportByData[i][j]);
                                }
                                continue;
                            }
                            case "TOLOC":
                            {
                                if (string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    goto Label_03D8;
                                }
                                movementDetailInfo.ToLoc = ImportByData[i][j];
                                continue;
                            }
                            case "VENDORID":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    movementDetailInfo.VendorID = ImportByData[i][j];
                                }
                                continue;
                            }
                            case "LOTTABLE01":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    movementDetailInfo.LotAttr01 = ImportByData[i][j];
                                }
                                continue;
                            }
                            case "LOTTABLE02":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    movementDetailInfo.LotAttr02 = ImportByData[i][j];
                                }
                                continue;
                            }
                            case "LOTTABLE03":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    movementDetailInfo.LotAttr03 = ImportByData[i][j];
                                }
                                continue;
                            }
                            case "LOTTABLE04":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    movementDetailInfo.LotAttr04 = ImportByData[i][j];
                                }
                                continue;
                            }
                            case "LOTTABLE05":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    movementDetailInfo.LotAttr05 = ImportByData[i][j];
                                }
                                continue;
                            }
                            case "LOTTABLE06":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    movementDetailInfo.LotAttr06 = ImportByData[i][j];
                                }
                                continue;
                            }
                            case "LOTTABLE07":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    movementDetailInfo.LotAttr07 = ImportByData[i][j];
                                }
                                continue;
                            }
                            case "LOTTABLE08":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    movementDetailInfo.LotAttr08 = ImportByData[i][j];
                                }
                                continue;
                            }
                            case "LOTTABLE09":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    movementDetailInfo.LotAttr09 = ImportByData[i][j];
                                }
                                continue;
                            }
                            case "LOTTABLE10":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    movementDetailInfo.LotAttr10 = ImportByData[i][j];
                                }
                                continue;
                            }
                            case "RECEIPTDATE":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    movementDetailInfo.ReceiptDate = DateTime.Parse(ImportByData[i][j]);
                                }
                                continue;
                            }
                            case "PRODUCEDATE":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    movementDetailInfo.ProduceDate = DateTime.Parse(ImportByData[i][j]);
                                }
                                continue;
                            }
                            case "EXPIRYDATE":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    movementDetailInfo.ExpiryDate = DateTime.Parse(ImportByData[i][j]);
                                }
                                continue;
                            }
                            case "EXTERNALLOT":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    movementDetailInfo.ExternalLot = ImportByData[i][j];
                                }
                                continue;
                            }
                            default:
                            {
                                continue;
                            }
                        }
                        movementDetailInfo.FromLoc = "";
                        continue;
                    Label_0339:
                        movementDetailInfo.Lot = "";
                        continue;
                    Label_0372:
                        movementDetailInfo.FromLpnID = "";
                        continue;
                    Label_03D8:
                        movementDetailInfo.ToLoc = "";
                    }
                    if (!skuDA.CheckSkuIDUnique(movementDetailInfo.OwnerID, movementDetailInfo.SkuID, this.dataBaseID[0]))
                    {
                        throw new Exception("货品" + movementDetailInfo.SkuID + "不存在!");
                    }
                    if (!locDA.CheckLocationIDUnique(movementDetailInfo.FromLoc, this.dataBaseID[0]))
                    {
                        throw new Exception("来源货位" + movementDetailInfo.FromLoc + "不存在!");
                    }
                    if (!locDA.CheckLocationIDUnique(movementDetailInfo.ToLoc, this.dataBaseID[0]))
                    {
                        throw new Exception("目标货位" + movementDetailInfo.ToLoc + "不存在!");
                    }
                    SkuInfo skuInfo = null;
                    if (!skuObjects.ContainsKey(movementDetailInfo.SkuID))
                    {
                        skuInfo = skuDA.GetSkuByID(movementDetailInfo.OwnerID, movementDetailInfo.SkuID, whLoginID);
                        skuObjects.Add(movementDetailInfo.SkuID, skuInfo);
                    }
                    else
                    {
                        skuObjects.TryGetValue(movementDetailInfo.SkuID, out skuInfo);
                    }
                    if (skuInfo != null)
                    {
                        MovementDetailInfo tmpMovementDetailInfo;
                        PackInfo packInfo;
                        if (skuInfo.IsActive != "Y")
                        {
                            throw new Exception(movementDetailInfo.SkuID + "( 货主" + movementDetailInfo.OwnerID + "  Sku(货品 " + movementDetailInfo.SkuID + ")未启用,导入失败");
                        }
                        RotationRuleInfo rotationRuleInfo = null;
                        if (!rotationRuleObjects.ContainsKey(skuInfo.RotationRuleID))
                        {
                            rotationRuleInfo = rotataionRuleDA.GetRotationRuleByID(skuInfo.RotationRuleID, whLoginID);
                            if (rotationRuleInfo == null)
                            {
                                throw new Exception("货品" + movementDetailInfo.SkuID + "没有指定周转规则,导入失败");
                            }
                            rotationRuleInfo.RotationRuleDetailInfo = rotataionRuleDetailDA.GetRotationRuleDetailListByID(rotationRuleInfo.RotationRuleID, whLoginID);
                            rotationRuleObjects.Add(rotationRuleInfo.RotationRuleID, rotationRuleInfo);
                        }
                        else
                        {
                            rotationRuleObjects.TryGetValue(skuInfo.RotationRuleID, out rotationRuleInfo);
                        }
                        string orderBy = outboundMgtBLL.OrganizeRotationRule(rotationRuleInfo);
                        LotAttributeInfo lotAttribute = new LotAttributeInfo {
                            OwnerID = movementDetailInfo.OwnerID,
                            SkuID = movementDetailInfo.SkuID,
                            LotAttr01 = movementDetailInfo.LotAttr01,
                            LotAttr02 = movementDetailInfo.LotAttr02,
                            LotAttr03 = movementDetailInfo.LotAttr03,
                            LotAttr04 = movementDetailInfo.LotAttr04,
                            LotAttr05 = movementDetailInfo.LotAttr05,
                            LotAttr06 = movementDetailInfo.LotAttr06,
                            LotAttr07 = movementDetailInfo.LotAttr07,
                            LotAttr08 = movementDetailInfo.LotAttr08,
                            LotAttr09 = movementDetailInfo.LotAttr09,
                            LotAttr10 = movementDetailInfo.LotAttr10,
                            ProduceDate = movementDetailInfo.ProduceDate,
                            ExpiryDate = movementDetailInfo.ExpiryDate,
                            ReceiptDate = movementDetailInfo.ReceiptDate,
                            ExternalLot = movementDetailInfo.ExternalLot,
                            VendorID = movementDetailInfo.VendorID
                        };
                        List<InventoryInfo> inventoryInfos = inventoryMgtDA.GetInventoryInfoByLotAttribute(lotAttribute, movementDetailInfo.FromLoc, movementDetailInfo.FromLpnID, orderBy, whLoginID);
                        if ((inventoryInfos != null) && (inventoryInfos.Count > 0))
                        {
                            decimal moveQty = movementDetailInfo.FromQty;
                            for (int k = 0; k < inventoryInfos.Count; k++)
                            {
                                InventoryInfo tmpInventoryInfo = null;
                                if (allocatedInventoryInfoList.ContainsKey(inventoryInfos[k].Lot + inventoryInfos[k].Loc + inventoryInfos[k].LpnID))
                                {
                                    tmpInventoryInfo = allocatedInventoryInfoList[inventoryInfos[k].Lot + inventoryInfos[k].Loc + inventoryInfos[k].LpnID];
                                }
                                else
                                {
                                    tmpInventoryInfo = inventoryInfos[k];
                                }
                                decimal invAvailableQty = (((tmpInventoryInfo.Qty - tmpInventoryInfo.AllocatedQty) - tmpInventoryInfo.ExpectedQty) - tmpInventoryInfo.PendingMoveIn) - tmpInventoryInfo.PickedQty;
                                if ((tmpInventoryInfo.Qty > 0M) && (invAvailableQty > 0M))
                                {
                                    if (moveQty <= 0M)
                                    {
                                        break;
                                    }
                                    tmpMovementDetailInfo = new MovementDetailInfo {
                                        MovementID = movementDetailInfo.MovementID,
                                        OwnerID = movementDetailInfo.OwnerID,
                                        SkuID = movementDetailInfo.SkuID,
                                        FromLoc = movementDetailInfo.FromLoc,
                                        FromLpnID = movementDetailInfo.FromLpnID,
                                        ToLoc = movementDetailInfo.ToLoc,
                                        ToLpnID = tmpInventoryInfo.LpnID,
                                        Lot = tmpInventoryInfo.Lot,
                                        FromQty = movementDetailInfo.FromQty
                                    };
                                    if (moveQty <= invAvailableQty)
                                    {
                                        tmpMovementDetailInfo.Qty = moveQty;
                                        moveQty = 0M;
                                    }
                                    else
                                    {
                                        tmpMovementDetailInfo.Qty = invAvailableQty;
                                        moveQty -= invAvailableQty;
                                    }
                                    tmpMovementDetailInfo.PackID = skuInfo.PackID;
                                    packInfo = null;
                                    if (!packInfoObjects.ContainsKey(skuInfo.PackID))
                                    {
                                        packInfo = packDA.GetPackByID(skuInfo.PackID, whLoginID);
                                        packInfoObjects.Add(skuInfo.PackID, packInfo);
                                    }
                                    else
                                    {
                                        packInfoObjects.TryGetValue(skuInfo.PackID, out packInfo);
                                    }
                                    tmpMovementDetailInfo.UomID = packInfo.PackUom1;
                                    tmpMovementDetailInfo.ReasonCode = "";
                                    tmpMovementDetailInfo.Remark = "";
                                    tmpMovementDetailInfo.Status = "New";
                                    tmpMovementDetailInfo.CreatedBy = this.pubParasInfo.UserID;
                                    tmpMovementDetailInfo.CreatedDate = DateTime.Now;
                                    tmpMovementDetailInfo.UpdatedBy = this.pubParasInfo.UserID;
                                    tmpMovementDetailInfo.UpdatedDate = DateTime.Now;
                                    movementDetailInfoList.Add(tmpMovementDetailInfo);
                                    tmpInventoryInfo.Qty -= tmpMovementDetailInfo.Qty;
                                    if (!allocatedInventoryInfoList.ContainsKey(tmpInventoryInfo.Lot + tmpInventoryInfo.Loc + tmpInventoryInfo.LpnID))
                                    {
                                        allocatedInventoryInfoList.Add(tmpInventoryInfo.Lot + tmpInventoryInfo.Loc + tmpInventoryInfo.LpnID, tmpInventoryInfo);
                                    }
                                    else
                                    {
                                        allocatedInventoryInfoList[tmpInventoryInfo.Lot + tmpInventoryInfo.Loc + tmpInventoryInfo.LpnID] = tmpInventoryInfo;
                                    }
                                }
                            }
                            if (moveQty == movementDetailInfo.FromQty)
                            {
                                tmpMovementDetailInfo = new MovementDetailInfo {
                                    MovementID = movementDetailInfo.MovementID,
                                    OwnerID = movementDetailInfo.OwnerID,
                                    SkuID = movementDetailInfo.SkuID,
                                    FromLoc = movementDetailInfo.FromLoc,
                                    FromLpnID = movementDetailInfo.FromLpnID,
                                    ToLoc = movementDetailInfo.ToLoc,
                                    ToLpnID = "",
                                    Lot = movementDetailInfo.Lot,
                                    FromQty = movementDetailInfo.FromQty,
                                    PackID = skuInfo.PackID
                                };
                                packInfo = null;
                                if (!packInfoObjects.ContainsKey(skuInfo.PackID))
                                {
                                    packInfo = packDA.GetPackByID(skuInfo.PackID, whLoginID);
                                    packInfoObjects.Add(skuInfo.PackID, packInfo);
                                }
                                else
                                {
                                    packInfoObjects.TryGetValue(skuInfo.PackID, out packInfo);
                                }
                                tmpMovementDetailInfo.UomID = packInfo.PackUom1;
                                tmpMovementDetailInfo.ReasonCode = "";
                                tmpMovementDetailInfo.Remark = "";
                                tmpMovementDetailInfo.Status = "New";
                                tmpMovementDetailInfo.CreatedBy = this.pubParasInfo.UserID;
                                tmpMovementDetailInfo.CreatedDate = DateTime.Now;
                                tmpMovementDetailInfo.UpdatedBy = this.pubParasInfo.UserID;
                                tmpMovementDetailInfo.UpdatedDate = DateTime.Now;
                                movementDetailInfoList.Add(tmpMovementDetailInfo);
                            }
                            continue;
                        }
                        tmpMovementDetailInfo = new MovementDetailInfo {
                            MovementID = movementDetailInfo.MovementID,
                            OwnerID = movementDetailInfo.OwnerID,
                            SkuID = movementDetailInfo.SkuID,
                            FromLoc = movementDetailInfo.FromLoc,
                            FromLpnID = movementDetailInfo.FromLpnID,
                            ToLoc = movementDetailInfo.ToLoc,
                            ToLpnID = "",
                            Lot = movementDetailInfo.Lot,
                            FromQty = movementDetailInfo.FromQty,
                            PackID = skuInfo.PackID
                        };
                        packInfo = null;
                        if (!packInfoObjects.ContainsKey(skuInfo.PackID))
                        {
                            packInfo = packDA.GetPackByID(skuInfo.PackID, whLoginID);
                            packInfoObjects.Add(skuInfo.PackID, packInfo);
                        }
                        else
                        {
                            packInfoObjects.TryGetValue(skuInfo.PackID, out packInfo);
                        }
                        tmpMovementDetailInfo.UomID = packInfo.PackUom1;
                        tmpMovementDetailInfo.ReasonCode = "";
                        tmpMovementDetailInfo.Remark = "";
                        tmpMovementDetailInfo.Status = "New";
                        tmpMovementDetailInfo.CreatedBy = this.pubParasInfo.UserID;
                        tmpMovementDetailInfo.CreatedDate = DateTime.Now;
                        tmpMovementDetailInfo.UpdatedBy = this.pubParasInfo.UserID;
                        tmpMovementDetailInfo.UpdatedDate = DateTime.Now;
                        movementDetailInfoList.Add(tmpMovementDetailInfo);
                    }
                }
                if ((movementDetailInfoList != null) && (movementDetailInfoList.Count > 0))
                {
                    int lineID = 0;
                    lineID = commonMethod.GetMaxLineID("MOVEMENT_DETAIL", strMovementID, whLoginID);
                    DataBase dataBase = new DataBase();
                    DbConnection conn = dataBase.connection;
                    conn.Open();
                    DbTransaction tran = conn.BeginTransaction();
                    try
                    {
                        for (i = 0; i < movementDetailInfoList.Count; i++)
                        {
                            lineID += 10;
                            movementDetailInfoList[i].LineID = lineID;
                            movementDetailDA.InsertMovementDetail(dataBase, tran, movementDetailInfoList[i], whLoginID);
                        }
                        tran.Commit();
                    }
                    catch (Exception exception1)
                    {
                        ex = exception1;
                        tran.Rollback();
                        CommonErrorHandler.ErrorHandler(ex, LogType.Error);
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
            catch (Exception exception2)
            {
                ex = exception2;
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return "";
        }

        public string UpdateMovementDetail(MovementDetailInfo obj)
        {
            string DetailID = string.Empty;
            try
            {
                if (!skuDA.CheckSkuIDUnique(obj.OwnerID, obj.SkuID, this.dataBaseID[0]))
                {
                    throw new Exception("货品" + obj.SkuID + "不存在!");
                }
                if (!locDA.CheckLocationIDUnique(obj.FromLoc, this.dataBaseID[0]))
                {
                    throw new Exception("来源货位" + obj.FromLoc + "不存在!");
                }
                if (!locDA.CheckLocationIDUnique(obj.ToLoc, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2330_003", this.pubParasInfo.UserLanguage, new List<string> { obj.ToLoc });
                }
                if (movementDetailDA.UpdateMovementDetail(obj, this.dataBaseID[0]) > 0)
                {
                    DetailID = obj.MovementID + " , " + obj.LineID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return DetailID;
        }
    }
}

